package com.timmy.chi.ui.fragment;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.timmy.chi.R;
import com.timmy.chi.ui.activity.LogInActivity;
import com.timmy.chi.utils.SPUtils;

import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;


/**
 * 用户页面
 */

public class UserFragment extends Fragment {


    Unbinder unbinder;
    private ImageView mLogin;
    private TextView mUsername;
    private TextView mPhone;
    private LinearLayout mLlUserinfo;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        View view = View.inflate(getContext(), R.layout.fragment_user, null);

        initView(view);

        setUserInfo();
        unbinder = ButterKnife.bind(this, view);
        return view;
    }

    private void setUserInfo() {
        mLogin.setVisibility(View.GONE);
        mLlUserinfo.setVisibility(View.VISIBLE);
        mUsername.setText("欢迎光临");
        String uersname = (String) SPUtils.get(getActivity(), "username", "");
        mPhone.setText(uersname);
    }

    private void initView(View view) {
        mLogin = (ImageView) view.findViewById(R.id.login);
        mUsername = (TextView) view.findViewById(R.id.username);
        mPhone = (TextView) view.findViewById(R.id.phone);
        mLlUserinfo = (LinearLayout) view.findViewById(R.id.ll_userinfo);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @OnClick(R.id.tv_user_setting)
    public void onViewClicked() {
        Dialog dialog = new AlertDialog.Builder(getActivity()).setTitle(null).setMessage("确定要退出登录吗？")
                .setNegativeButton("再想想", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        dialogInterface.dismiss();
                    }
                })
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                        Intent  intent = new Intent(getActivity(), LogInActivity.class);
                        startActivity(intent);
                        getActivity().finish();

                    }
                }).show();
        dialog.setCanceledOnTouchOutside(false);


    }
}
